﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using System.Web.Routing;
using System.Text;
using System.Data;

namespace DreamBox.Net.Web.Http
{
    public  static class HttpInvoke
    {
        /// <summary>
        /// 启动程序
        /// </summary>
        /// <param name="RequestContext"></param>
        public static void Init(RequestContext RequestContext)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("NameSpace", RequestContext.RouteData.DataTokens["Namespace"] == null ? "" : RequestContext.RouteData.DataTokens["Namespace"].ToString());
            dic.Add("BoxName", RequestContext.RouteData.Values["box"].ToString());
            //
            InvokeBox ibox = new InvokeBox(RequestContext);
            ibox.ActionBox = dic;

            ibox.Invoke();

            //接收执行返回值
            Object result = ibox.ResultObject;
            //当返回结果为null时 则返回的是void类型
            if (result != null)
            {
                if (result.GetType().IsSubclassOf(typeof(ActionResult)))
                {
                    ActionResult actionResult = (ActionResult)result;
                    RequestContext.HttpContext.Response.Write(actionResult.ResultObject);
                }
                else
                {
                    RequestContext.HttpContext.Response.Write(result);
                }
            }
        }
        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="RequestContext"></param>
        public static bool Competence(RequestContext RequestContext)
        {
            var result = true;
            ////权限检测需过滤掉信息提示页,头部以及底部请求
            //string url=RequestContext.RouteData.DataTokens["Namespace"] == null ? "" : RequestContext.RouteData.DataTokens["Namespace"].ToString() + "/";
            //url+=RequestContext.RouteData.Values["box"].ToString();
            //if (url.Contains("message") || url.Contains("header") || url.Contains("footer"))
            //{
            //    result = true;
            //}
            //else
            //{
            //    //权限检测
            //    string sqlsys = "select ConfigID,RegisterOpen,RegisterOpenMsg,SiteOpen,SiteOpenMsg,IsLogin,IsLoginMsg from dbox_Config where ConfigID=1";

            //    Data.DbAction action = new Data.DbAction();

            //    DataTable dt = action.Select(sqlsys, true);

            //    if (dt.Rows.Count > 0)
            //    {
            //        if (!bool.Parse(dt.Rows[0]["SiteOpen"].ToString()))
            //        {
            //            RequestContext.HttpContext.Response.Redirect("/message?msg=" + dt.Rows[0]["SiteOpenMsg"] + "&url=&display=0");
            //        }
            //        if (!bool.Parse(dt.Rows[0]["IsLogin"].ToString()) && RequestContext.HttpContext.Session["UserID"] == null)
            //        {
            //            RequestContext.HttpContext.Response.Redirect("/message?msg=" + dt.Rows[0]["IsLoginMsg"] + "&url=");
            //        }
            //        if (RequestContext.HttpContext.Session["GID"] != null)
            //        {
            //            string sqlu = "select * from dbox_v_usergroupcompetence where GID=" + RequestContext.HttpContext.Session["GID"];
            //            //获取到权限数据
            //            if (RequestContext.HttpContext.Session["GroupCompetenceData"] == null)
            //            {
            //                RequestContext.HttpContext.Session["GroupCompetenceData"] = action.Select(sqlu, true);
            //            }

            //            //
            //            DataRow[] rows = (RequestContext.HttpContext.Session["GroupCompetenceData"] as DataTable).Select("Url='" + url + "'");
            //            if (rows.Length <= 0)
            //            {
            //                result = false;
            //            }
            //            else
            //            {
            //                result = true;
            //            }
            //        }
            //    }
            //    else
            //    {
            //        result = false;
            //    }
            //}
            return result;
        }
    }
}
